package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.CustomsDataLogMapper;
import com.zhentao.pojo.CustomsDataLog;
import com.zhentao.service.CustomsDataLogService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author 李佳隆
* @description 针对表【customs_data_log(海关数据采集模块统一日志表：覆盖全流程操作、数据、异常、应用行为)】的数据库操作Service实现
* @createDate 2025-08-27 09:50:25
*/
@Service
public class CustomsDataLogServiceImpl extends ServiceImpl<CustomsDataLogMapper, CustomsDataLog>
    implements CustomsDataLogService{

    @Override
    public List<CustomsDataLog> getPageList(Integer page, Integer size, Map<String, Object> queryParams) {
        // 创建分页对象
        Page<CustomsDataLog> pageObj = new Page<>(page, size);
        
        // 构建查询条件
        QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
        
        if (queryParams != null) {
            // 业务模块条件
            if (queryParams.get("bizModule") != null) {
                queryWrapper.eq("biz_module", queryParams.get("bizModule"));
            }
            
            // 操作类型条件
            if (queryParams.get("bizType") != null) {
                queryWrapper.eq("biz_type", queryParams.get("bizType"));
            }
            
            // 操作人条件
            if (queryParams.get("operator") != null) {
                queryWrapper.eq("operator", queryParams.get("operator"));
            }
            
            // 时间范围条件
            if (queryParams.get("startTime") != null) {
                queryWrapper.ge("operate_time", queryParams.get("startTime"));
            }
            if (queryParams.get("endTime") != null) {
                queryWrapper.le("operate_time", queryParams.get("endTime"));
            }
        }
        
        // 按操作时间倒序排列
        queryWrapper.orderByDesc("operate_time");
        
        // 执行分页查询
        IPage<CustomsDataLog> result = this.page(pageObj, queryWrapper);
        
        return result.getRecords();
    }

    @Override
    public Long countByModule(Integer bizModule) {
        QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("biz_module", bizModule);
        return this.count(queryWrapper);
    }

    @Override
    public Long countByType(Integer bizType) {
        QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("biz_type", bizType);
        return this.count(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> getOperatorStatistics() {
        // 使用MyBatis-Plus的聚合查询功能
        QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("operator, count(*) as count")
                   .groupBy("operator")
                   .orderByDesc("count");
        
        List<Map<String, Object>> maps = this.listMaps(queryWrapper);
        return maps;
    }

    @Override
    public List<Map<String, Object>> getLogTrend(String startTime, String endTime, String granularity) {
        List<Map<String, Object>> trendData = new ArrayList<>();
        
        try {
            String dateFormat;
            String groupFormat;
            
            // 根据时间粒度设置格式
            switch (granularity) {
                case "hour":
                    dateFormat = "yyyy-MM-dd HH";
                    groupFormat = "DATE_FORMAT(operate_time, '%Y-%m-%d %H')";
                    break;
                case "day":
                    dateFormat = "yyyy-MM-dd";
                    groupFormat = "DATE_FORMAT(operate_time, '%Y-%m-%d')";
                    break;
                case "month":
                    dateFormat = "yyyy-MM";
                    groupFormat = "DATE_FORMAT(operate_time, '%Y-%m')";
                    break;
                default:
                    dateFormat = "yyyy-MM-dd";
                    groupFormat = "DATE_FORMAT(operate_time, '%Y-%m-%d')";
            }
            
            // 构建查询条件
            QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.select(groupFormat + " as time_period, count(*) as count")
                       .ge("operate_time", startTime)
                       .le("operate_time", endTime)
                       .groupBy("time_period")
                       .orderBy(true, true, "time_period");
            
            List<Map<String, Object>> maps = this.listMaps(queryWrapper);
            trendData.addAll(maps);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return trendData;
    }

    @Override
    public List<CustomsDataLog> getAllByParams(Map<String, Object> queryParams) {
        // 构建查询条件
        QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
        
        if (queryParams != null) {
            if (queryParams.get("bizModule") != null) {
                queryWrapper.eq("biz_module", queryParams.get("bizModule"));
            }
            if (queryParams.get("bizType") != null) {
                queryWrapper.eq("biz_type", queryParams.get("bizType"));
            }
            if (queryParams.get("operator") != null) {
                queryWrapper.eq("operator", queryParams.get("operator"));
            }
            if (queryParams.get("startTime") != null) {
                queryWrapper.ge("operate_time", queryParams.get("startTime"));
            }
            if (queryParams.get("endTime") != null) {
                queryWrapper.le("operate_time", queryParams.get("endTime"));
            }
        }
        
        queryWrapper.orderByDesc("operate_time");
        
        return this.list(queryWrapper);
    }

    @Override
    public int cleanupBefore(String beforeDate) {
        QueryWrapper<CustomsDataLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("operate_time", beforeDate);
        
        long count = this.count(queryWrapper);
        this.remove(queryWrapper);
        
        return (int) count;
    }

}




